package com.example.exercise;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.exercise.MysqlUtil.DBService;
import com.example.exercise.MysqlUtil.FindExist;

import java.util.Random;


public class MainActivity extends AppCompatActivity {

    private TextView qiandao,jilu,chaxun,luru,guashi,about,task,update,sign2,ma;
    SharedPreferences sharedPreferences;
    SharedPreferences getacc;
    Random random = new Random();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        qiandao = findViewById(R.id.sign_t);
        jilu = findViewById(R.id.jilu_t);
        luru =  findViewById(R.id.signup_t);
        guashi = findViewById(R.id.yishi_t);
        chaxun = findViewById(R.id.cardsearch_t);
        about = findViewById(R.id.about_t);
        task = findViewById(R.id.textView45);
        task.setVisibility(View.GONE);
        update =findViewById(R.id.cardupdate);
        sign2 = findViewById(R.id.sign_t3);
        ma = findViewById(R.id.ma);
        getacc = getSharedPreferences("Acc",MODE_PRIVATE);
        sharedPreferences = getSharedPreferences("state",MODE_PRIVATE);


        new Thread(new Runnable() {
            @Override
            public void run() {

                if (FindExist.getRecord() == 0){
                    Looper.prepare();
                    showClearDialog();
                    Looper.loop();

                }
            }
        }).start();
        if (sharedPreferences.getInt("state",1) == 1 )
        {
            task.setVisibility(View.GONE);
        }else {
            task.setVisibility(View.VISIBLE);
        }

        if (getacc.getString("Acc","").equals("user"))
        {

            update.setTextColor(Color.GRAY);
            guashi.setTextColor(Color.GRAY);
            ma.setTextColor(Color.GRAY);
            sign2.setTextColor(Color.GRAY);
        }
        ma.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (getacc.getString("Acc","").equals("user"))
                {
                    Toast.makeText(getApplicationContext(),"你没有权限",Toast.LENGTH_SHORT).show();
                }
                else
                {
                    String number1 =String.valueOf(random.nextInt( 10 )+1);
                    String number2 =String.valueOf(random.nextInt( 10 ));
                    String number3 =String.valueOf(random.nextInt( 10 ));
                    String number4 =String.valueOf(random.nextInt( 10 ));
                    final int number5 = Integer.parseInt(number1+number2+number3+number4);
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            if (DBService.SetUserMa(number5)!=-1)
                            {
                                Looper.prepare();
                                showMaDialog(number5);
                                Looper.loop();
                            }
                            else
                            {
                                Looper.prepare();
                                showMaFailDialog();
                                Looper.loop();
                            }

                        }
                    }).start();

                }
            }
        });

        sign2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (getacc.getString("Acc","").equals("user"))
                {
                    Toast.makeText(getApplicationContext(),"你没有权限",Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Intent intent = new Intent(getApplicationContext(),Sign2.class);
                    startActivity(intent);
                }
//                if (getacc.getString("Acc","").equals("user"))
//                {
//                    Toast.makeText(getApplicationContext(),"请输入随机码",Toast.LENGTH_SHORT).show();
//                    Inputpwd();
//
//                }
//                else
//                {
//                    Intent intent = new Intent(getApplicationContext(),Sign2.class);
//                    startActivity(intent);
//                }


            }
        });
        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getacc.getString("Acc","").equals("user"))
                {
                    Toast.makeText(getApplicationContext(),"你没有权限",Toast.LENGTH_SHORT).show();
                }
                else
                    {
                        Intent intent = new Intent(getApplicationContext(),Del.class);
                        startActivity(intent);
                    }
            }
        });

        luru.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(),Cardjoin.class);
                startActivity(intent);
            }
        });

        qiandao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(),Sign.class);
                startActivity(intent);
            }
        });

        chaxun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(),Cardjoin.class);
                startActivity(intent);
            }
        });

        jilu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(),SignRecord.class);
                startActivity(intent);
            }
        });

        about.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(),About.class);
                startActivity(intent);
            }
        });


        guashi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (getacc.getString("Acc","").equals("user"))
                {
                    Toast.makeText(getApplicationContext(),"你没有权限",Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Intent intent = new Intent(getApplicationContext(),LostCard.class);
                    startActivity(intent);
                }

            }
        });


    }

    private void showClearDialog() {

        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(MainActivity.this);
        normalDialog.setTitle("清除提示");
        normalDialog.setMessage("软件检测到有签到记录，是否转到清除");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(getApplicationContext(),SignRecord.class);
                        startActivity(intent);
                    }
                });
        normalDialog.setNegativeButton("关闭",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do

                    }
                });
        // 显示
        normalDialog.show();
    }

    private void showMaDialog(int ma) {

        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(MainActivity.this);
        normalDialog.setTitle("成功提示");
        normalDialog.setMessage("手动签到随机码已重置为"+ma+"\n请谨慎告知他人");
        normalDialog.setNegativeButton("关闭",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do

                    }
                });
        // 显示
        normalDialog.show();
    }

    private void showMaFailDialog() {

        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(MainActivity.this);
        normalDialog.setTitle("失败提示");
        normalDialog.setMessage("手动签到随机码重置失败，请重试");
        normalDialog.setNegativeButton("关闭",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do

                    }
                });
        // 显示
        normalDialog.show();
    }



    private void   Inputpwd() {
        /*@setView 装入一个EditView
         */

        final EditText editText = new EditText(MainActivity.this);
        AlertDialog.Builder inputDialog =
                new AlertDialog.Builder(MainActivity.this);
        inputDialog.setTitle("请输入随机码").setView(editText);
        inputDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        if (editText.getText().toString().trim().equals("")){
                            Toast.makeText(getApplicationContext(),
                                    "输入为空，重新输入",
                                    Toast.LENGTH_SHORT).show();
                            Inputpwd();


                        }else {
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    int temp = DBService.getUserMa();
                                    System.out.println(temp);
                                    if (temp == 0){
                                        Looper.prepare();
                                        Toast.makeText(getApplicationContext(),
                                                "校验失败，可能网络问题",
                                                Toast.LENGTH_SHORT).show();
                                        Looper.loop();
                                    }
                                    else {
                                        if (Integer.parseInt(editText.getText().toString().trim()) == temp){

                                            Intent intent = new Intent(getApplicationContext(),Sign2.class);
                                            startActivity(intent);

                                        }
                                        else {
                                            Looper.prepare();
                                            Toast.makeText(getApplicationContext(),
                                                    "密码错误",
                                                    Toast.LENGTH_SHORT).show();
                                            Looper.loop();
                                            Inputpwd();
                                        }
                                    }


                                }
                            }).start();

                        }
                    }
                }).show();


    }



}